<template>
  <div class="cyberpunk-login-wrap">
    <!-- 赛博朋克背景 -->
    <div class="cyberpunk-bg">
      <!-- 全息网格 -->
      <div class="hologrid"></div>
      <!-- 霓虹光条 -->
      <div class="neon-strips">
        <div class="neon-strip neon-strip-1"></div>
        <div class="neon-strip neon-strip-2"></div>
        <div class="neon-strip neon-strip-3"></div>
        <div class="neon-strip neon-strip-4"></div>
        <div class="neon-strip neon-strip-5"></div>
      </div>
      <!-- 全息广告牌 -->
      <div class="holographic-billboards">
        <div class="billboard billboard-1">
          <span>// LOGIN //</span>
        </div>
        <div class="billboard billboard-2">
          <span>// ACCESS //</span>
        </div>
        <div class="billboard billboard-3">
          <span>// ENTER //</span>
        </div>
      </div>
      <!-- 数字雨 -->
      <div class="matrix-rain"></div>
      <!-- 脉冲圆圈 -->
      <div class="pulse-circles">
        <div class="pulse-circle pulse-circle-1"></div>
        <div class="pulse-circle pulse-circle-2"></div>
        <div class="pulse-circle pulse-circle-3"></div>
      </div>
      <!-- 扫描线 -->
      <div class="scanlines"></div>
      <!-- 故障效果 -->
      <div class="glitch-overlay"></div>
    </div>

    <div class="login-container">
      <div class="login-panel">
        <!-- 顶部LOGO区域 -->
        <div class="panel-header">
          <div class="logo-section">
            <div class="cyber-logo">
              <div class="logo-core"></div>
              <div class="logo-ring"></div>
              <div class="logo-grid"></div>
            </div>
            <h2 class="cyber-title">
              <span class="title-glow">ERP</span>
              <span class="title-text">管理系统</span>
            </h2>
            <p class="cyber-subtitle">进入数字未来</p>
          </div>
        </div>

        <!-- 登录表单 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <div class="input-wrapper">
              <el-input
                v-model="loginForm.username"
                placeholder="用户名"
                size="large"
                prefix-icon="User"
                class="cyber-input"
                @focus="inputFocus"
                @blur="inputBlur"
              />
              <div class="input-border"></div>
              <div class="input-glow"></div>
            </div>
          </el-form-item>

          <el-form-item prop="password">
            <div class="input-wrapper">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                size="large"
                prefix-icon="Lock"
                class="cyber-input"
                show-password
                @focus="inputFocus"
                @blur="inputBlur"
              />
              <div class="input-border"></div>
              <div class="input-glow"></div>
            </div>
          </el-form-item>

          <el-form-item>
            <div class="remember-forgot">
              <el-checkbox v-model="rememberMe" class="cyber-checkbox">
                <span>记住我</span>
              </el-checkbox>
              <a class="cyber-link" @click="forgotPassword">忘记密码？</a>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="cyber-button"
              :loading="loading"
              @click="handleLogin"
            >
              <span class="button-text">登录</span>
              <span class="button-bg"></span>
              <span class="button-glow"></span>
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 底部链接 -->
        <div class="panel-footer">
          <p class="footer-text">
            还没有账号？
            <a class="cyber-link" @click="register">立即注册</a>
          </p>
        </div>
      </div>

      <!-- 粒子装饰 -->
      <div class="particles">
        <div class="particle" v-for="i in 50" :key="i"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const store = useUserStore()

const emit = defineEmits(['login'])

const loginFormRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const response = await store.login(loginForm)

    if (response.code === 200) {
      ElMessage.success(response.msg || '登录成功')
      emit('login', response)
      router.push('/dashboard')
    } else {
      ElMessage.error(response.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '网络请求失败，请重试')
  } finally {
    loading.value = false
  }
}

const register = () => {
  ElMessage.info('注册功能开发中...')
}

const forgotPassword = () => {
  ElMessage.info('忘记密码功能开发中...')
}

const inputFocus = (e) => {
  const wrapper = e.target.closest('.input-wrapper')
  if (wrapper) {
    wrapper.classList.add('active')
  }
}

const inputBlur = (e) => {
  const wrapper = e.target.closest('.input-wrapper')
  if (wrapper && !e.target.value) {
    wrapper.classList.remove('active')
  }
}

onMounted(() => {
  // 创建数字雨效果
  createMatrixRain()
})

const createMatrixRain = () => {
  const matrixRain = document.querySelector('.matrix-rain')
  if (!matrixRain) return
  
  const chars = '01アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン'
  const fontSize = 16
  const columns = Math.floor(window.innerWidth / fontSize)
  
  const drops = Array(columns).fill(1)
  
  const draw = () => {
    const width = matrixRain.offsetWidth
    const height = matrixRain.offsetHeight
    
    matrixRain.innerHTML = ''
    matrixRain.style.width = width + 'px'
    matrixRain.style.height = height + 'px'
    
    for (let i = 0; i < drops.length; i++) {
      const text = chars.charAt(Math.floor(Math.random() * chars.length))
      const drop = document.createElement('div')
      drop.className = 'matrix-drop'
      drop.textContent = text
      drop.style.left = i * fontSize + 'px'
      drop.style.animationDelay = drops[i] * 0.05 + 's'
      drop.style.opacity = Math.random()
      matrixRain.appendChild(drop)
    }
  }
  
  draw()
}
</script>

<style scoped>
.cyberpunk-login-wrap {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #0a0a0f;
  font-family: 'Courier New', monospace;
}

/* 赛博朋克背景 */
.cyberpunk-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

/* 全息网格 */
.hologrid {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(rgba(0, 255, 255, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 255, 0.03) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: gridMove 20s linear infinite;
}

@keyframes gridMove {
  0% { transform: translate(0, 0); }
  100% { transform: translate(50px, 50px); }
}

/* 霓虹光条 */
.neon-strips {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.neon-strip {
  position: absolute;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, 
    transparent,
    #ff00ff 50%,
    #00ffff 70%,
    transparent
  );
  filter: blur(2px);
  animation: neonFlow 3s ease-in-out infinite;
}

.neon-strip-1 { top: 10%; animation-delay: 0s; }
.neon-strip-2 { top: 30%; animation-delay: 0.6s; }
.neon-strip-3 { top: 50%; animation-delay: 1.2s; }
.neon-strip-4 { top: 70%; animation-delay: 1.8s; }
.neon-strip-5 { top: 90%; animation-delay: 2.4s; }

@keyframes neonFlow {
  0%, 100% { opacity: 0.3; transform: translateX(-100%); }
  50% { opacity: 1; transform: translateX(100%); }
}

/* 全息广告牌 */
.holographic-billboards {
  position: absolute;
  width: 100%;
  height: 100%;
}

.billboard {
  position: absolute;
  font-size: 60px;
  font-weight: bold;
  color: transparent;
  -webkit-text-stroke: 1px #00ffff;
  text-shadow: 
    0 0 20px #00ffff,
    0 0 40px #00ffff,
    0 0 60px #00ffff;
  opacity: 0.2;
  animation: hologram 4s ease-in-out infinite;
}

.billboard-1 { top: 5%; left: 5%; animation-delay: 0s; }
.billboard-2 { top: 25%; right: 10%; animation-delay: 1.3s; }
.billboard-3 { bottom: 10%; left: 15%; animation-delay: 2.6s; }

@keyframes hologram {
  0%, 100% { opacity: 0.2; transform: translateY(0); }
  50% { opacity: 0.4; transform: translateY(-20px); }
}

/* 数字雨 */
.matrix-rain {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0.2;
  pointer-events: none;
}

.matrix-drop {
  position: absolute;
  color: #00ff00;
  font-family: monospace;
  font-size: 16px;
  animation: matrixFall linear infinite;
}

@keyframes matrixFall {
  to { transform: translateY(100vh); }
}

/* 脉冲圆圈 */
.pulse-circles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.pulse-circle {
  position: absolute;
  border: 2px solid rgba(0, 255, 255, 0.3);
  border-radius: 50%;
  animation: pulseExpand 3s ease-out infinite;
}

.pulse-circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 20%;
  animation-delay: 0s;
}

.pulse-circle-2 {
  width: 300px;
  height: 300px;
  top: 60%;
  right: 15%;
  animation-delay: 1s;
}

.pulse-circle-3 {
  width: 150px;
  height: 150px;
  bottom: 20%;
  left: 10%;
  animation-delay: 2s;
}

@keyframes pulseExpand {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1.5); opacity: 0; }
}

/* 扫描线 */
.scanlines {
  position: absolute;
  width: 100%;
  height: 100%;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 255, 255, 0.03) 2px,
    rgba(0, 255, 255, 0.03) 4px
  );
  animation: scanlineMove 10s linear infinite;
  pointer-events: none;
}

@keyframes scanlineMove {
  0% { background-position: 0 0; }
  100% { background-position: 0 100%; }
}

/* 故障效果 */
.glitch-overlay {
  position: absolute;
  width: 100%;
  height: 100%;
  background: 
    linear-gradient(90deg, 
      rgba(255, 0, 255, 0) 0%, 
      rgba(255, 0, 255, 0.01) 50%, 
      rgba(255, 0, 255, 0) 100%
    );
  animation: glitch 8s infinite;
  pointer-events: none;
}

@keyframes glitch {
  0%, 90%, 100% { transform: translate(0); }
  91% { transform: translate(-2px, 2px); }
  92% { transform: translate(2px, -2px); }
  93% { transform: translate(-2px, 2px); }
  94% { transform: translate(2px, -2px); }
  95% { transform: translate(0); }
}

/* 登录容器 */
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

/* 登录面板 */
.login-panel {
  position: relative;
  width: 450px;
  background: rgba(10, 10, 15, 0.7);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.3);
  border-radius: 0;
  padding: 50px 40px;
  box-shadow: 
    0 0 40px rgba(0, 255, 255, 0.2),
    0 0 80px rgba(255, 0, 255, 0.1),
    inset 0 0 40px rgba(0, 255, 255, 0.05);
  animation: panelGlow 4s ease-in-out infinite;
}

@keyframes panelGlow {
  0%, 100% { 
    box-shadow: 
      0 0 40px rgba(0, 255, 255, 0.2),
      0 0 80px rgba(255, 0, 255, 0.1),
      inset 0 0 40px rgba(0, 255, 255, 0.05);
  }
  50% { 
    box-shadow: 
      0 0 60px rgba(0, 255, 255, 0.4),
      0 0 120px rgba(255, 0, 255, 0.2),
      inset 0 0 60px rgba(0, 255, 255, 0.1);
  }
}

.panel-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-section {
  position: relative;
}

.cyber-logo {
  position: relative;
  width: 100px;
  height: 100px;
  margin: 0 auto 20px;
}

.logo-core {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 2px solid #00ffff;
  box-shadow: 
    0 0 20px #00ffff,
    inset 0 0 20px #00ffff;
  animation: coreRotate 4s linear infinite;
}

.logo-ring {
  position: absolute;
  width: 70px;
  height: 70px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 1px dashed #ff00ff;
  border-radius: 50%;
  animation: ringRotate 6s linear infinite reverse;
}

.logo-grid {
  position: absolute;
  width: 80px;
  height: 80px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: 
    linear-gradient(rgba(0, 255, 255, 0.3) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 255, 0.3) 1px, transparent 1px);
  background-size: 10px 10px;
}

@keyframes coreRotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes ringRotate {
  0% { transform: translate(-50%, -50%) rotate(0deg); }
  100% { transform: translate(-50%, -50%) rotate(360deg); }
}

.cyber-title {
  font-size: 32px;
  font-weight: bold;
  margin: 0 0 10px;
}

.title-glow {
  color: #00ffff;
  text-shadow: 
    0 0 10px #00ffff,
    0 0 20px #00ffff,
    0 0 30px #00ffff;
}

.title-text {
  color: #ff00ff;
  text-shadow: 
    0 0 10px #ff00ff,
    0 0 20px #ff00ff;
}

.cyber-subtitle {
  font-size: 14px;
  color: rgba(0, 255, 255, 0.6);
  margin: 0;
  letter-spacing: 2px;
}

/* 登录表单 */
.login-form {
  width: 100%;
}

.input-wrapper {
  position: relative;
  margin-bottom: 30px;
}

.cyber-input {
  width: 100%;
}

:deep(.el-input__wrapper) {
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(0, 255, 255, 0.3);
  box-shadow: none;
  transition: all 0.3s;
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(0, 255, 255, 0.6);
  box-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

:deep(.el-input.is-focus .el-input__wrapper) {
  border-color: #00ffff;
  box-shadow: 0 0 15px rgba(0, 255, 255, 0.5);
}

:deep(.el-input__inner) {
  color: #00ffff;
  font-family: 'Courier New', monospace;
}

:deep(.el-input__inner::placeholder) {
  color: rgba(0, 255, 255, 0.5);
}

.input-border {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #00ffff, transparent);
  transform: scaleX(0);
  transition: transform 0.3s;
}

.input-wrapper.active .input-border {
  transform: scaleX(1);
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.cyber-checkbox :deep(.el-checkbox__label) {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

.cyber-checkbox :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #00ffff;
  border-color: #00ffff;
}

.cyber-link {
  color: #00ffff;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.3s;
  position: relative;
}

.cyber-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 1px;
  background: #00ffff;
  transition: width 0.3s;
}

.cyber-link:hover::after {
  width: 100%;
}

.cyber-link:hover {
  text-shadow: 0 0 10px #00ffff;
}

/* 登录按钮 */
.cyber-button {
  width: 100%;
  height: 50px;
  position: relative;
  background: transparent;
  border: 2px solid #00ffff;
  color: #00ffff;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 2px;
  overflow: hidden;
  transition: all 0.3s;
}

.cyber-button:hover {
  color: #0a0a0f;
  text-shadow: none;
}

.cyber-button:hover .button-bg {
  width: 100%;
}

.button-text {
  position: relative;
  z-index: 2;
}

.button-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, #00ffff, #ff00ff);
  transition: width 0.4s;
}

:deep(.cyber-button.is-loading) {
  pointer-events: none;
}

.panel-footer {
  text-align: center;
  margin-top: 20px;
}

.footer-text {
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
}

/* 粒子装饰 */
.particles {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
}

.particle {
  position: absolute;
  width: 2px;
  height: 2px;
  background: #00ffff;
  border-radius: 50%;
  box-shadow: 0 0 6px #00ffff;
  animation: particleFloat 15s infinite;
}

@keyframes particleFloat {
  0% {
    transform: translate(0, 100vh) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translate(100px, -100vh) rotate(360deg);
    opacity: 0;
  }
}
</style>

<style>
/* 全局样式覆盖 */
.particle:nth-child(1) { left: 10%; animation-delay: 0s; }
.particle:nth-child(2) { left: 20%; animation-delay: 1.5s; }
.particle:nth-child(3) { left: 30%; animation-delay: 3s; }
.particle:nth-child(4) { left: 40%; animation-delay: 4.5s; }
.particle:nth-child(5) { left: 50%; animation-delay: 6s; }
.particle:nth-child(6) { left: 60%; animation-delay: 7.5s; }
.particle:nth-child(7) { left: 70%; animation-delay: 9s; }
.particle:nth-child(8) { left: 80%; animation-delay: 10.5s; }
.particle:nth-child(9) { left: 90%; animation-delay: 12s; }
</style>

